package com.scaffold.catalog.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.scaffold.catalog.dao.CatalogDAO;
import com.scaffold.catalog.entity.Catalog;

@Service
@Transactional(rollbackFor = Exception.class)
public class CatalogService {

	@Autowired
	private CatalogDAO catalogDAO;
	
	public Catalog getCatalog(long id) {
		return this.catalogDAO.findById(id);
	}
	
	public void save(Catalog catalog){
		Date now = new Date();
		if(catalog.getId()==null){
			catalog.setCreated(now);
			catalog.setModified(now);
			this.catalogDAO.save(catalog);
		}else{
			catalog.setModified(now);
			this.catalogDAO.update(catalog);
		}
	}
	
	public void update(Catalog catalog){
		Date now = new Date();
		catalog.setModified(now);
		this.catalogDAO.update(catalog);
	}

	public void delete(Catalog catalog){
		this.catalogDAO.delete(catalog);
	}

	public List<Catalog> findByParentId(Long parentId) {
		return this.catalogDAO.findByParentId(parentId);
	}

}
